package com.fau.wmbj.server.storage;

import com.fau.wmbj.server.service.base.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 存储
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2024-03-14 15:16
 */
@Service
public class StorageConfigServiceImpl extends BaseServiceImpl<StorageConfig, Long> implements StorageConfigService {

    @Resource
    private List<Storage> storages = new ArrayList<>();
    @Resource
    private Map<String, Storage> storageMap = new HashMap<>();

    @Override
    public List<Storage> getStoragePlugins() {
        Collections.sort(storages);
        return storages;
    }

    @Override
    public List<Storage> getStoragePlugins(boolean isEnabled) {
        List<Storage> result = new ArrayList<>();
        CollectionUtils.select(storages, new Predicate() {
            @Override
            public boolean evaluate(Object object) {
                Storage storagePlugin = (Storage) object;
                boolean enabled = storagePlugin.getIsEnabled();
                return enabled == isEnabled;
            }
        }, result);
        Collections.sort(result);
        return result;
    }

    @Override
    public Storage getStorage(String id) {
        return storageMap.get(id);
    }
}
